//
//  LJBasicTableViewController.m
//  百思不得姐
//
//  Created by 刘鹿杰 on 16/8/28.
//  Copyright © 2016年 刘鹿杰. All rights reserved.
//

#import "LJBasicTableViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <MJExtension.h>
#import "LJEssionTableCell.h"
#import "LJTopicsModel.h"


static NSString * const essionCellIndentifile = @"essionCellIndentifile";

@interface LJBasicTableViewController ()
/** 帖子数据 */
@property (nonatomic, strong) NSMutableArray *topics;
/** page 加载数据页数 */
@property (nonatomic, assign) NSInteger page;
/** 判断返回字段是否加载成功 */
@property (nonatomic, strong) NSMutableDictionary *params;
/** 加载下一页数据时, 需要这个数字 */
@property (nonatomic, copy) NSString *maxtime;
@end

@implementation LJBasicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置UITableView
    [self setUpTableView];
    
    // 设置刷新控件
    [self setUpRefrash];
    
}

-(void)setUpTableView{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LJEssionTableCell class]) bundle:nil] forCellReuseIdentifier:essionCellIndentifile];
    self.tableView.backgroundColor = LJGlobalBg;
    
}


-(void)setUpRefrash{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDate)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDate)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark -- 加载数据

-(void)loadNewDate{
    // 让下拉刷新结束
    [self.tableView.mj_footer endRefreshing];
    // 参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"data";
    params[@"type"] = @(self.type);
    self.params = params;
    
    // 发送请求
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.params != params) return ;
        
        // 存储加载的最大时间
        self.maxtime = responseObject[@"info"][@"maxtime"];
        
        self.topics = [LJTopicsModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        // 刷新TableView
        [self.tableView reloadData];
        
        // 结束刷新操作
        [self.tableView.mj_header endRefreshing];
        
        // 上拉加载清零操作
        self.page = 0;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (self.params != params) return ;
        
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
        
    }];
    
}

-(void)loadMoreDate{
    // 结束上拉刷新
    [self.tableView.mj_header endRefreshing];
    // 参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"data";
    params[@"type"] = @(self.type);
    NSInteger page = self.page + 1;
    params[@"page"] = @(page);
    params[@"maxtime"] = self.maxtime;
    self.params = params;
    // 发送请求
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (self.params != params) return ;
        
        // 存储最大的数字
        self.maxtime = responseObject[@"info"][@"maxtime"];
        
        // 提取新新加载的数据
        NSArray *topicsArray = [LJTopicsModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        // 添加新的数据
        [self.topics  addObjectsFromArray:topicsArray];
        
        // 刷新数据
        [self.tableView reloadData];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        
        // 赋值数据
        self.page = page;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (self.params != params) return ;
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        
    }];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    self.tableView.mj_footer.hidden = (self.topics.count == 0);
    
    return self.topics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LJEssionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:essionCellIndentifile];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.topicsModel = self.topics[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LJTopicsModel * topicModel =  self.topics[indexPath.row];
   
    return topicModel.cellHight;
}

@end
